//
//  Image.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/3.
//  Copyright © 2018 LKGood. All rights reserved.
//

import Foundation

struct RemoteImage {
  
  enum Size {
    case original, medium, thumb
  }
  
  var origianlPath: String?
  var mediumPath: String?
  var thumbPath: String?
  
  init(origianlPath: String?, mediumPath: String?, thumbPath: String?) {
    self.origianlPath = origianlPath
    self.mediumPath   = mediumPath
    self.thumbPath    = thumbPath
  }
  
  init(dictionary: [String: Any]) {
    origianlPath  = dictionary["original"] as? String
    mediumPath    = dictionary["medium"] as? String
    thumbPath     = dictionary["thumb"] as? String
  }
  
  private func url(path: String?) -> URL? {
    if path != nil {
        if path!.hasPrefix("http") {
            return URL(string: path!)
        }
        else{
            return URL(string: APIURLStrings.imagePrefix + path!)
        }
    } else {
      return nil
    }
  }
  
  func getURL(size: Size) -> URL? {
    switch size {
    case .original:
      return url(path: origianlPath)
    case .medium:
      return url(path: mediumPath)
    case .thumb:
      return url(path: thumbPath)
    }
  }
  
  func getDictionary() -> [String: Any] {
    return [
      "origianl": origianlPath ?? "",
      "medium": mediumPath ?? "",
      "thumb": thumbPath ?? ""
    ]
  }
}
